package com.jcark.ui.widget.datepicker

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import com.bigkoo.pickerview.listener.OnDismissListener
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.contrarywind.view.WheelView
import com.jcark.ui.R
import com.jcark.ui.databinding.PopupDatePickerBinding
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import java.util.Calendar

/**
 * Description:日期时间选择器弹窗 <br>
 * @author King.Zi<br>
 * @date 2025/03/12<br>
 * Email: 78688689@qq.com
 */
class DatePickerPopupWindow(
    context: Context,
    private val dateTimeParameter: DateTimeParameter,
    private val timeSelectListener: OnTimeSelectListener
) : BottomPopupView(context) {

    lateinit var binding: PopupDatePickerBinding
    private var mTimePickerView: MTimePickerView? = null
    private var mOnDismissListener: OnDismissListener? = null

    override fun getImplLayoutId(): Int {
        return R.layout.popup_date_picker
    }

    override fun onCreate() {
        super.onCreate()
        binding = PopupDatePickerBinding.bind(findViewById(R.id.parent))

        val initDate = Calendar.getInstance()
        if (!TextUtils.isEmpty(dateTimeParameter.initDate)) {
            val dateArray = dateTimeParameter.initDate.split("-".toRegex())
            initDate.set(dateArray[0].toInt(), dateArray[1].toInt() - 1, dateArray[2].toInt())
        }

        val startDate = Calendar.getInstance()
        if (!TextUtils.isEmpty(dateTimeParameter.startDate)) {
            val dateArray = dateTimeParameter.startDate.split("-".toRegex())
            startDate.set(dateArray[0].toInt(), dateArray[1].toInt() - 1, dateArray[2].toInt())
        }

        val endDate = Calendar.getInstance()
        if (!TextUtils.isEmpty(dateTimeParameter.endDate)) {
            val dateArray = dateTimeParameter.endDate.split("-".toRegex())
            endDate.set(dateArray[0].toInt(), dateArray[1].toInt() - 1, dateArray[2].toInt())
        }

        //时间选择器
        val timePickerBuilder = MTimePickerBuilder(context, timeSelectListener)
            .setLayoutRes(R.layout.dialog_date_picker_view) { rootView: View ->
                rootView.findViewById<TextView>(R.id.tvCancel).setOnClickListener { view ->
                    mOnDismissListener?.onDismiss(true)
                    dismiss()
                }
                rootView.findViewById<TextView>(R.id.tvConfirm).setOnClickListener { v ->
                    mTimePickerView?.returnData()
                    dismiss()
                }
            }
            .setType(dateTimeParameter.type)
            .setContentTextSize(15)
            .setDate(initDate)
            .setRangDate(startDate, endDate)
            .isDialog(false)
            .setDividerType(WheelView.DividerType.FILL)
            .setLineSpacingMultiplier(2f) //每行item间距
            .setOutSideColor(Color.parseColor("#B3000000")) //弹窗以外背景色
            .setOutSideCancelable(false)
            .isCenterLabel(dateTimeParameter.isCenterLabel)
            .setItemVisibleCount(5)//最多显示条数
            .setDecorView(binding.parent)
        if (dateTimeParameter.isShowLabel) {
            timePickerBuilder.setLabel(
                getString(R.string.pickerview_year),
                getString(R.string.pickerview_month),
                getString(R.string.pickerview_day),
                getString(R.string.pickerview_hours),
                getString(R.string.pickerview_minutes),
                getString(R.string.pickerview_seconds)
            )
        } else {
            timePickerBuilder.setLabel("", "", "", "", "", "")
        }

        mTimePickerView = timePickerBuilder.build()
        //系统返回键监听屏蔽掉
        mTimePickerView?.setKeyBackCancelable(false)
        mTimePickerView?.show()
    }

    /**
     * 监听窗口关闭，返回true表示点击了【取消】按钮
     */
    fun setOnDismissListener(dismissListener: OnDismissListener) {
        mOnDismissListener = dismissListener
    }

    private fun getString(strResId: Int): String {
        return context.resources.getString(strResId)
    }

    fun showPopupWindow() {
        getPopupBuilder(context).asCustom(this).show()
        return
    }

    private fun getPopupBuilder(context: Context): XPopup.Builder {
        return XPopup.Builder(context)
            .hasShadowBg(true)
            .autoDismiss(false)
            .isDestroyOnDismiss(true)
            .dismissOnTouchOutside(false)
            .enableDrag(false)
            .isTouchThrough(false)
            .isViewMode(true)
            .moveUpToKeyboard(false)
            .hasShadowBg(true)
    }
}